package com.dqy.twserver.server;

import com.dqy.twserver.server.codec.DataDecode;
import com.dqy.twserver.server.codec.DataEncode;
import com.dqy.twserver.server.handle.ServerInboundHandle;
import com.dqy.twserver.server.handle.ehandle.*;
import com.dqy.twserver.server.handle.ehandle.frame.GameEventFrameSycnHandle;
import com.dqy.twserver.server.handle.ehandle.hook.GameEventPlayUnregisteredHandle;
import com.dqy.twserver.server.handle.ehandle.mutil.*;
import com.dqy.twserver.server.support.Constans;
import com.dqy.twserver.server.support.SupportMap;
import com.dqy.twserver.server.support.data.PlayerInfo;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//初始化游戏服务端

@Component
public class GameServer implements   ApplicationListener<ContextRefreshedEvent>{

    static Logger logger = LoggerFactory.getLogger(GameServer.class);

    @Value("${tws.port}")
    int port;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    private NioEventLoopGroup wk;
    private NioEventLoopGroup boss;
    private ServerBootstrap serverBootstrap;
    public GameServer(){
        serverBootstrap=new ServerBootstrap();
        wk = new NioEventLoopGroup(1);
        boss = new NioEventLoopGroup(1);

        serverBootstrap.group(boss,wk).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<NioSocketChannel>() {
            protected void initChannel(NioSocketChannel ch) throws Exception {
                ch.pipeline().addLast(new DataDecode());
                ch.pipeline().addLast(new DataEncode());
                ch.pipeline().addLast(new ServerInboundHandle());
            }
        });
        logger.info("服务端初始化完成...");
    }

    public void start(){
        try {
            serverBootstrap.bind(port).sync();
            logger.info("服务端启动完成...");
        } catch (InterruptedException e) {
            logger.info("服务端启动失败...原因:{}",e);
        }
    }

    //初始化
    public void init(){
        SupportMap.roomMap=new ConcurrentHashMap<>();
        SupportMap.playerMap=new ConcurrentHashMap<>();
        SupportMap.playerDataMap=new ConcurrentHashMap<>();
        SupportMap.gameEventHandleMap=new ConcurrentHashMap<>();
        SupportMap.channelIdPlayMap=new ConcurrentHashMap<>();

        //测试代码，测试直接拿这个登录,免去测试时频繁注册
        PlayerInfo info=new PlayerInfo("test1","1");
        PlayerInfo info2=new PlayerInfo("test2","1");
        SupportMap.playerDataMap.put("test1",info);
        SupportMap.playerDataMap.put("test2",info2);

        logger.info("supportMap 初始化完毕...");

        Map<Integer, GameEventHandle> gameEventHandleMap = SupportMap.gameEventHandleMap;
        gameEventHandleMap.put(Constans.EVENT_TYPE_LOGING,new GameEventLogingHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_REGIST,new GameEventRegistHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_GET_ROOM,new GameEventGetRoomHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_CREATE_ROOM,new GameEventCreateRoomHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_SEND_MSG,new GameEventSendMsgHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_OUT_ROOM,new GameEventOutRoomHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_GAME_START,new GameEventStartHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_CREATE_TERR,new GameEventCreateTerr());

        gameEventHandleMap.put(Constans.EVENT_TYPE_MUTIL_SOCKET_REGIST, new GameEventMutilRegistHandle());

        gameEventHandleMap.put(Constans.EVENT_TYPE_READI, new GameEventReadiHandle());
        gameEventHandleMap.put(Constans.EVENT_TYPE_IN_ROOM, new GameEventInRoomHandle());

        gameEventHandleMap.put(Constans.EVENT_TYPE_GAME_FRAME_SYCN, new GameEventFrameSycnHandle());

        gameEventHandleMap.put(Constans.EVENT_TYPE_UNREGISTER_HOOK,new GameEventPlayUnregisteredHandle());
        logger.info("事件处理器注册完毕...");


    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        event.getApplicationContext().getBean(GameServer.class).start();
        init();
    }
}
